package javabase.schoolwork.day6_12;

/**
 * @author fance
 * @date 18-6-11 下午3:34
 */
public class Person implements Comparable<Person> {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Person o) {
        if (this.age > o.age) {
            return -1;
        } else if (this.age < o.age) {
            return 1;
        } else {
            return 0;
        }
    }


    @Override
    public int hashCode() {
        return this.name.hashCode() * this.age;
    }

    @Override
    public boolean equals(Object obj) {
       if (this == obj) {
           return true;
       }
       if (!(obj instanceof Person)) {
           return false;
       }
       Person p = (Person) obj;
       if (this.name.equals(p.name) && this.age == p.age) {
           return true;
       } else {
           return false;
       }
    }
}
